package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.NewsClassVO;
import com.wanxi.dao.NewsClassDao;
//import com.wanxi.dao.impl.NewsClassDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.DeviceClassEntity;
import com.wanxi.entity.NewsClassEntity;
import com.wanxi.entity.NewsEntity;
import com.wanxi.entity.UserEntity;
import com.wanxi.service.NewsClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 19:57
 */
@Service("newsClassService")
public class NewsClassServiceImpl implements NewsClassService {
    @Autowired
    private  NewsClassDao newsClassDao;
   @Autowired
   private NewsClassEntity newsClassEntity;

    Jedis jedis = new Jedis();
    public ResultDTO findAll(NewsClassVO newsClassVO) {
//        NewsClassEntity newsClass = new NewsClassEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(newsClassVO,newsClassEntity);
        int count = newsClassDao.count(newsClassEntity);
        List<NewsClassEntity> list = new ArrayList<>();
        if (newsClassEntity.getPage() != null && newsClassEntity.getPage() == 1 && jedis.exists("newsClassList")){
            String jedisNewsClassList = jedis.get("newsClassList");
            //反序列化数组，方法2
            JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, NewsClassEntity.class);
            try {
                list = mapper.readValue(jedisNewsClassList, javaType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else {
            list = newsClassDao.findAll(newsClassEntity);
            try {
                String newsClassList = mapper.writeValueAsString(list);
                jedis.set("newsClassList",newsClassList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return ResultDTO.getResult("", count, list);
    }
    public ResultDTO add(NewsClassVO newsClassVO) {
//        NewsClassEntity newsClassEntity = new NewsClassEntity();
        BeanUtils.copyProperties(newsClassVO,newsClassEntity);
        int count = newsClassDao.add(newsClassEntity);
        jedis.del("newsClassList");
        return ResultDTO.getResult(count);
    }
    public ResultDTO delete(NewsClassVO newsClassVO) {
//        NewsClassEntity newsClassEntity = new NewsClassEntity();
        BeanUtils.copyProperties(newsClassVO, newsClassEntity);
        int delete = newsClassDao.delete(newsClassEntity);
        jedis.del("newsClassList");
        return ResultDTO.getResult(delete);
    }
    public ResultDTO findById(NewsClassVO newsClassVO) {
//        NewsClassEntity newsClassEntity = new NewsClassEntity();
        BeanUtils.copyProperties(newsClassVO,newsClassEntity);
        NewsClassEntity byId = newsClassDao.findById(newsClassEntity);
        return ResultDTO.getResult(byId);
    }
    public ResultDTO update(NewsClassVO newsClassVO) {
//        NewsClassEntity newsClassEntity = new NewsClassEntity();
        BeanUtils.copyProperties(newsClassVO, newsClassEntity);
        int update = newsClassDao.update(newsClassEntity);
        jedis.del("newsClassList");
        return ResultDTO.getResult(update);
    }
}
